package com.joven.product.service.infrastructure.repository.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.joven.product.service.business.domain.model.entity.Product;
import com.joven.product.service.business.domain.model.entity.ProductAttribute;
import com.joven.product.service.business.domain.model.entity.ProductDetail;
import com.joven.product.service.business.domain.model.entity.id.ProductAttributeId;
import com.joven.product.service.business.domain.model.entity.id.ProductDetailId;
import com.joven.product.service.business.domain.model.entity.id.ProductId;
import com.joven.product.service.infrastructure.converter.ProductConverter;
import com.joven.product.service.infrastructure.persistence.db.entity.ProductAttributeDO;
import com.joven.product.service.infrastructure.persistence.db.entity.ProductDO;
import com.joven.product.service.infrastructure.persistence.db.entity.ProductDetailDO;
import com.joven.product.service.infrastructure.repository.ProductRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品Repository接口实现
 *
 * @author Joven
 * @date 2022-01-06 18:21:27
 */
@Component
public class ProductRepositoryImpl extends BaseRepositoryImpl<ProductId, Product, ProductDO> implements ProductRepository {
    private BaseRepositoryImpl<ProductDetailId, ProductDetail, ProductDetailDO> productDetailRepository;
    private BaseRepositoryImpl<ProductAttributeId, ProductAttribute, ProductAttributeDO> productAttributeRepository;

    public ProductRepositoryImpl(IService<ProductDO> productDao, IService<ProductDetailDO> detailDao, IService<ProductAttributeDO> attributeDao) {
        super(productDao, ProductConverter.INSTANCE);
        productDetailRepository = new BaseRepositoryImpl<>(detailDao, ProductConverter.ProductDetailAssembler.INSTANCE);
        productAttributeRepository = new BaseRepositoryImpl<>(attributeDao, ProductConverter.ProductAttributeAssembler.INSTANCE);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Product data) {
        super.save(data);
        productDetailRepository.save(data.getDetail());
        productAttributeRepository.saveBatch(data.getAttributes());
        return true;
    }

    @Override
    public boolean saveBatch(List<Product> datas) {
        if (CollectionUtils.isEmpty(datas)) {
            return false;
        }
        super.saveBatch(datas);
        List<ProductDetail> details = datas.stream().map(Product::getDetail).collect(Collectors.toList());
        productDetailRepository.saveBatch(details);
        List<ProductAttribute> attrs = datas.stream().map(Product::getAttributes).filter(Objects::nonNull)
                .flatMap(Collection::stream).collect(Collectors.toList());
        productAttributeRepository.saveBatch(attrs);
        return true;
    }
}
